package cn.android.weblog;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;

import cn.android.weblog.domain.Catagory;
import cn.android.weblog.domain.User;
import cn.android.weblog.redisdata.CURRENT;

public class AuthorActivity extends AppCompatActivity {

    private ImageView back;
    private Button catagory;
    private TextView id,name,sex,email,time,status,latelyTime;
    private User author;
    private PopupMenu pm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_author);

        back=(ImageView)findViewById(R.id.author_back);
        catagory=(Button)findViewById(R.id.author_catagory);
        id=(TextView)findViewById(R.id.author_id);
        name=(TextView)findViewById(R.id.author_name);
        sex=(TextView)findViewById(R.id.author_sex);
        email=(TextView)findViewById(R.id.author_email);
        time=(TextView)findViewById(R.id.author_time);
        status=(TextView)findViewById(R.id.author_status);
        latelyTime=(TextView)findViewById(R.id.author_lately);

        // 初始化页面
        initAuthor();

        // 显示用户分类列表
        catagory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 显示菜单项
                pm.show();
            }
        });

        // 返回上一页
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    /**
     * 初始化页面
     */
    private void initAuthor(){
        author= CURRENT.AUTHOR;
        // 给控件赋值
        id.setText(author.getUserId().toString());
        name.setText(author.getUsername());
        sex.setText(author.getSex());
        email.setText(author.getEmail());
        time.setText(author.getJoinDate().toString());
        latelyTime.setText(author.getLastTime().toString());
        status.setText(author.getStatus()==1?"正常使用":"已注销");
        initCatagory();
    }

    /**
     * 初始化用户分类
     */
    private void initCatagory(){
        pm=new PopupMenu(AuthorActivity.this,catagory);
        Menu menu=pm.getMenu();
        pm.getMenuInflater().inflate(R.menu.author_catagory,menu);
        // 将分类全部添加到菜单项中
        for(Catagory c:author.getCatagoryName()){
            // 1-组别、2-菜单项id、3-菜单项顺序、4-菜单项内容
            menu.add(0,c.getCatagoryId(),0,c.getCatagoryName());
        }
        // 添加单击数据项事件
        pm.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                // 当选择某一个分类的时候，更新页面值
                changeActivity(item);
                return true;
            }
        });
    }

    /**
     * 当选择分类后跳转到相应页面
     */
    private void changeActivity(MenuItem item){
        CURRENT.CATAGORY=new Catagory(item.getItemId(),item.getTitle().toString());
        // 跳转到分类页面
        startActivity(new Intent(AuthorActivity.this,CatagoryPassageActivity.class));
    }

}
